// pages/order/order.js
import Dialog from '/@vant/weapp/dialog/dialog';
import { Order } from '../../model/order.js';
import { Config } from '../../utils/config.js';
var order = new Order();
var appInst =  getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    showShen: false,
    subLoading: false,
    hidden: false,
    orderList: [],
    orderTwo: [],
    orderThree: [],
    shen_url: "",
    active_order: 0,
    active: 1,
    icon: {
      home: '../../utils/image/home.png',
      home_active: '../../utils/image/home-active.png',
      order: '../../utils/image/order.png',
      order_active: '../../utils/image/order-active.png',
      user: '../../utils/image/user.png',
      user_active: '../../utils/image/user-active.png'
    }
  },
  /**
   * 切换菜单
   */
  onChangeTabbar(e){
    if(e.detail == 0) {
      appInst.goHome();
    }else  if(e.detail == 2) {
      wx.switchTab({
        url: '../user/user',
      })
    }
  },
  /**
   * 打开产品详情 
   */
  openWin(e) {
    wx.navigateTo({
        url: `/pages/product/product?id=${e.currentTarget.dataset.id}`,
    })
  },
  /**
   * 显示审核二维码
   */
  onClickShenShow(e) {
    const id = e.currentTarget.dataset.id;
    this.setData({
      shen_url: `${Config.qrUrl}order_${id}.png`,
      showShen: true
    })
  },
  /**
   * 隐藏审核二维码
   */
  onClickShenHide() {
    this.setData({
      shen_url: '',
      showShen: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    wx.hideTabBar();
    wx.showLoading({
      title: '加载中',
    });
    this.getOrder();
    if(appInst.globalData.appstorename!='美业团')
      appInst.settitle();
  },
  getOrder(){
      order.getProductOrderList((result)=>{
        var  orderList = []
        var  orderTwo = []
        var orderThree = []
        result.forEach((item)=>{
          item.loading = false
          if(item.status == 2) {
            orderTwo.push(item)
          }
          if(item.status == 3) {
            orderThree.push(item)
          }
          orderList.push(item)
        })
        this.setData({
          orderList: orderList,
          orderTwo: orderTwo,
          orderThree: orderThree,
          hidden: true,
        })
        wx.hideLoading()
      })
  },
  // 去支付
  goPay(e) {
    if (e.currentTarget.dataset.loading) {
        return false;
      }
      var orderList = this.data.orderList
      orderList.forEach((item)=>{
        if (e.currentTarget.dataset.id == item.id) {
          item.loading = true
        }else {
          item.loading = false
        }
      })
      this.setData({
        orderList: orderList
      });
      var self = this
    order.preOrder({ out_trade_no:e.currentTarget.dataset.pay ,payment: 1}, (isVild,preData)=>{
      console.log(isVild,preData);
      if (!isVild) {
        orderList.forEach((item) => {
          item.loading = false
        })
        this.setData({
          orderList: orderList
        });
        for (var tmp in preData.msg) {
            wx.showToast({
              title: preData.msg[tmp],
              icon: 'none',
              duration: 2000
            })
            return false
          }
        }else{
        if(preData.pay_type == 2){
          wx.hideLoading();
          orderList.forEach((item) => {
              item.loading = false
          })
          self.setData({
            orderList: orderList
          });
          wx.showModal({
            title: '温馨提示',
            content: '支付成功',
            showCancel: false,
            confirmText: '知道了',
            confirmColor: '#F3474E',
            success:function(res){
              if (res.confirm){
                self.getOrder()
              }
            }
          })
        }else{
          wx.requestPayment({
            timeStamp: preData.timeStamp.toString(),
            nonceStr: preData.nonceStr,
            package: preData.package,
            signType: preData.signType,
            paySign: preData.paySign,
            success: function (res) {
              wx.hideLoading();
              orderList.forEach((item) => {
                item.loading = false
              })
              self.setData({
                orderList: orderList
              });
              wx.showModal({
                title: '温馨提示',
                content: '支付成功',
                showCancel: false,
                confirmText: '知道了',
                confirmColor: '#F3474E',
                success:function(res){
                  if (res.confirm){
                    self.getOrder()
                  }
                }
              })
            },
            fail: function (error) {
              console.log(error)
              wx.hideLoading();
              orderList.forEach((item) => {
                item.loading = false
              })
              self.setData({
                orderList: orderList
              });
              if (error.errMsg == "requestPayment:fail cancel") {
                wx.showToast({
                  title: '取消了支付',
                  icon: 'none',
                  duration: 2000
                })
                return false
              } else {
                wx.hideLoading();
                orderList.forEach((item) => {
                  item.loading = false
                })
                self.setData({
                  orderList: orderList
                });
                wx.showToast({
                  title: '支付错误',
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
        }
    })
  },
  /**
   * 删除订单
   */
  delOrder(e) {
    Dialog.confirm({
      title: '确认删除此订单？',
      message: '删除后将不可恢复，若有需要请重新下单',
    })
    .then(() => {
      // on confirm
      order.deleteProductOrder({id: e.currentTarget.dataset.id},(isVild,res) => {
        if(isVild) {
            this.getOrder()
        }else {
          for (var tmp in preData.msg) {
            wx.showToast({
              title: preData.msg[tmp],
              icon: 'none',
              duration: 2000
            })
            return false
          }
        }
      })
    })
    .catch(() => {
      // on cancel
    });
  }
  
})